﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TagPropagator.Controls;

namespace TagPropagator.Views
{
    /// <summary>
    /// Interaction logic for GraphControl.xaml
    /// </summary>
    public partial class GraphControl : UserControl
    {
        private Random _rand = new Random();
        private List<NodeControl> _nodesControls;
        private List<EdgeControl> _edgesControls;
        private int _row = 1;
        private int _column = 1;

        public static readonly DependencyProperty GraphProperty = DependencyProperty.Register("Graph", typeof(Graph), typeof(GraphControl));

        public Graph Graph
        {
            get { return (Graph)GetValue(GraphProperty); }
            set
            {
                SetValue(GraphProperty, value);
                MyCanvas.Children.Clear();
                _nodesControls.Clear();
                _edgesControls.Clear();
                _row = 1; _column = 1;
                DrawNodes();
                DrawEdges();
            }
        }

        public GraphControl()
        {
            InitializeComponent();
            _nodesControls = new List<NodeControl>();
            _edgesControls = new List<EdgeControl>();
            MyCanvas.Height = 1000;
        }

        private void DrawNodes()
        {
            if (Graph == null) return;
            foreach (Node node in Graph.Nodes)
            {
                DrawNode(node);
            }
        }

        public void DrawNode(Node node)
        {
            NodeControl control = new NodeControl();
            control.Node = node;
            _nodesControls.Add(control);
            control.ID = node.ID;
            control.Image = node.Image;
            MyCanvas.Children.Add(control);
            //double xVariation = _rand.NextDouble()*50;
            //double yVariation = _rand.NextDouble()*50;
            Canvas.SetTop(control, (_row * 150));
            control.Y = _row * 150;
            Canvas.SetLeft(control, (_column * 150));
            control.X = _column * 150;
            _column++;
            if (_column >= 6)
            {
                _column = 1;
                _row++;
            }
            if (MyCanvas.Height < control.Y)
                MyCanvas.Height += 500;
        }

        private void DrawEdges()
        {
            foreach (NodeControl node in _nodesControls)
                DrawEdge(node);
        }

        private void DrawEdge(NodeControl nodeControl)
        {
            Node node = nodeControl.Node as Node;
            foreach (KeyValuePair<int, double> pair in node.Neighbours)
            {
                NodeControl nodeControl2 = null;
                foreach (NodeControl ctrl in _nodesControls)
                {
                    if (((Node)ctrl.Node).ID == pair.Key)
                    {
                        nodeControl2 = ctrl;
                        break;
                    }
                }
                if (nodeControl2 == null)
                    continue;
                EdgeControl edgeControl = new EdgeControl();
                edgeControl.X1 = nodeControl.X + 12.5;
                edgeControl.Y1 = nodeControl.Y + 12.5;

                edgeControl.X2 = nodeControl2.X + 12.5;
                edgeControl.Y2 = nodeControl2.Y + 12.5;
                MyCanvas.Children.Add(edgeControl);
                _edgesControls.Add(edgeControl);
            }
        }


    }
}
